﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MDIApplication.Classes;


namespace MDIApplication
{
    public partial class Form1 : Form
    {
        string staffPosition;
        public Form1(string staffPositionLogin )
        {
            Thread.Sleep(2000);
            staffPosition = staffPositionLogin;
            InitializeComponent();
        }

        #region Form_Load
        Home homeChild;
        private void Form1_Load(object sender, EventArgs e)
        {        
            //Default Form
            homeChild = new Home();
            homeChild.Location = new Point(0, 0);
            // Set the Parent Form of the Child window.
            homeChild.MdiParent = this;
            // Display the form.
            homeChild.Show();
            homeChild.Activate();
            if (staffPosition == "Administrator")
            {
                menuStrip3.Visible = false;
                menuStrip1.Visible = false;
            }
            else if(staffPosition == "Manager")
            {
                menuStrip1.Visible = false;
                menuStrip2.Visible = false;
            }
            else if (staffPosition == "FirstLine")
            {
                menuStrip3.Visible = false;
                menuStrip2.Visible = false;
            }        
        }
        #endregion Form_Load


        private void homeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (homeChild == null)
            {
                homeChild = new Home();
                homeChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                homeChild.MdiParent = this;
                // Display the form.
                homeChild.Show();
                homeChild.Activate();
            }
        }


        UpdateAcct updateAcctChild;
        private void toolStripMenuItem26_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (updateAcctChild == null)
            {
                updateAcctChild = new UpdateAcct();
                updateAcctChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                updateAcctChild.MdiParent = this;
                // Display the form.
                updateAcctChild.Show();
                updateAcctChild.Activate();
            }
        }


        AllBookingscs allBookingChild;
        private void toolStripMenuItem15_Click(object sender, EventArgs e)
        {         
            CloseAllForms();
            if (allBookingChild == null)
            {
                allBookingChild = new AllBookingscs();
                allBookingChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                allBookingChild.MdiParent = this;
                // Display the form.
                allBookingChild.Show();
                allBookingChild.Activate();
            }
        }
        Update updateBookingChild;
        private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (updateBookingChild == null)
             {
                 updateBookingChild = new Update();
                 updateBookingChild.Location = new Point(0, 0);
                 // Set the Parent Form of the Child window.
                 updateBookingChild.MdiParent = this;
                 // Display the form.
                 updateBookingChild.Show();
                 updateBookingChild.Activate();
            }
        }

        Report reportChild;
        private void toolStripMenuItem25_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (reportChild == null)
            {
                reportChild = new Report(staffPosition);
                reportChild.Location = new Point(0, 0);
                reportChild.MdiParent = this;
                reportChild.Show();
                reportChild.Activate();
            }

        }
        CreateNewAcct accountChild;
        private void toolStripMenuItem27_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (accountChild == null)
            {
                accountChild = new CreateNewAcct();
                accountChild.Location = new Point(0, 0);
                accountChild.MdiParent = this;
                accountChild.Show();
                accountChild.Activate();
            }
        }


        private void toolStripMenuItem17_Click(object sender, EventArgs e)
        {

        }

        GuestManagement guestManagementChild;
        private void toolStripMenuItem20_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (guestManagementChild == null)
            {
                guestManagementChild = new GuestManagement();
                guestManagementChild.Location = new Point(0, 0);
                guestManagementChild.MdiParent = this;
                guestManagementChild.Show();
                guestManagementChild.Activate();
            }
        }

        Report report;
        private void toolStripMenuItem24_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (report == null)
            {
                report = new Report(staffPosition);
                report.MdiParent = this;
                report.Location = new Point(0, 0);
                report.Show();
            }
        }

        UpdateRoomInfo updateRoomInfoChild;
        private void toolStripMenuItem21_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (updateRoomInfoChild == null)
            {
                updateRoomInfoChild = new UpdateRoomInfo();
                updateRoomInfoChild.MdiParent = this;
                updateRoomInfoChild.Location = new Point(0, 0);
                // Display the form.
                updateRoomInfoChild.Show();
                updateRoomInfoChild.Activate();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            closeForm();
        }


        #region CloseAllForms
        private void CloseAllForms()
        {
            Form[] formToClose = null;
            int i = 1;
            foreach (Form form in Application.OpenForms)
            {
                if (form != this && form.IsMdiChild ) //this is form1
                {
                    Array.Resize(ref formToClose, i);
                    formToClose[i - 1] = form;
                    i++;
                }
            }
            if (formToClose != null)
            {
                for (int j = 0; j < formToClose.Length; j++)
                {
                    formToClose[j].Close();                
                }
                homeChild = null;
                updateBookingChild = null;
                reportChild = null;
                accountChild = null;
                guestManagementChild = null;
                allBookingChild = null;
                updateAcctChild = null;
                report = null;
                updateRoomInfoChild = null;
            }
        }
        #endregion CloseAllForms

        #region NotifyIcon
        private void executeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Activate();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {

        }
        #endregion

        bool showResult = false;
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

            if (showResult == false)
            {
                DialogResult dialogResult = MessageBox.Show("Are you sure to close Applicaiton?", "Close Application", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    showResult = true;
                    Application.Exit();                  
                }
            }
        }


        void closeForm()
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure to log out?", "Log Out", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {           
                Login lg = new Login();
                lg.Show();
                lg.Activate();
                this.Hide();
            }   
        }





       
    }
}
